package com.example.doma.contraller;


import com.example.doma.pojo.dorm;
import com.example.doma.pojo.student;
import com.example.doma.service.dormService;
import com.example.doma.service.studentService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.List;

@Controller
public class dormcontraller {

    @Autowired
    dormService dormService;

    @Autowired
    studentService studentService;


    @GetMapping("/doma")
    public  String regers(Model model , HttpServletRequest request)
    {
        Integer Page=0;
        if(request.getParameter("page")==null){
             Page=1;
        }
        else {
            Page=Integer.valueOf(request.getParameter("page"));
        }
            PageInfo<dorm> dormPageInfo = dormService.queryAllPages(Page, 30);
            model.addAttribute("dorms",dormPageInfo.getList());
            model.addAttribute("pages",dormPageInfo.getNavigatePages());
            model.addAttribute("pa",Page);
        System.out.println(Page);
        return "admin-doma";
    }


    @RequestMapping("/doma/add")
    public  String domaAdd()
    {
        return "admin-doma-add";
    }

    //添加操作
    @PostMapping("/doma/add")
    public  String Add(dorm dorm,Model model )
    {
        System.out.println(dorm);
        if(dormService.isdorm(dorm.getApart_num(),dorm.getDormi_num()))
        {
            model.addAttribute("msg","寝室已存在");
            return "admin-doma-add";
        }
         dormService.addDorm(dorm);
         model.addAttribute("msg","添加成功");
        return "admin-doma-add";
    }

    //修改操作
    @GetMapping("/doma/modify")
    public  String modify(HttpServletRequest request,Model model)
    {
        System.out.println(request.getParameter("apart_num")+request.getParameter("dormi_num"));
        dorm serdrom = dormService.getdrom(request.getParameter("apart_num"), Integer.parseInt(request.getParameter("dormi_num")));
        model.addAttribute("dorm",serdrom);
        return "admin-doma-modify";
    }

    //提交修改后的表
    @PostMapping("/doma/modify")
    public  String Tmodify(dorm dorm ,Model model)
    {
        System.out.println("提交修改宿舍信息请求参数"+dorm);
        dormService.updatedorm(dorm);
        model.addAttribute("msg","修改成功");
        return "admin-doma-modify.html";
    }

    //删除操作
    @GetMapping("/doma/dele")
    public String   Deledoma(HttpServletRequest request)
    {
        System.out.println(request.getParameter("apart_num")+"..."+request.getParameter("dormi_num"));
        dormService.deledorm(request.getParameter("apart_num"),Integer.valueOf(request.getParameter("dormi_num")));
        return "redirect:/doma";

    }
    //搜索功能
    @PostMapping("/doma/souch")
    public  String souchDoma(@RequestParam("apart_num") String apart_num,@RequestParam("dormi_num") int dormi_num ,Model model)
    {
        System.out.println(apart_num+dormi_num);
        List<dorm> dorm = dormService.getDormByapart_numAnddormi_num(apart_num, dormi_num);
        model.addAttribute("dorms",dorm);
        model.addAttribute("pages",1);
        return "admin-doma";
    }

    @RequestMapping("/dom")
    public  String ge(Model model)
    {
        model.addAttribute("pa",99);
        return "fenye";
    }

    @GetMapping ("/dos")
    public  String sss(HttpServletRequest request)
    {
        System.out.println(request.getParameter("pa"));
        return "fenye";
    }
    //查看寝室成员信息
    @RequestMapping("/doma/students")
    public  String students(@PathParam("apart_num") String apart_num,@PathParam("dormi_num") int dormi_num,Model model)
    {
        System.out.println("查看宿舍成员信息、"+apart_num+dormi_num);
        List<student> students = studentService.getstudentsBydorm(apart_num, dormi_num);
        System.out.println(students);
        model.addAttribute("Doma_students",students);
        return "admin-doma-name";
    }
   //批量添加功能
    @RequestMapping("/doma/adds")
    public  String adds()
    {
        return "admin-doma-excel2";
    }
    //批量添加，提交功能
    @PostMapping("/doma/uploadExcel")
    @ResponseBody
    public  String uploadExcel(@RequestParam("file") MultipartFile file)throws IOException
    {
        System.out.println("---------");
        System.out.println("收到批量添加宿舍信息请求");
        HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
        dormService.AddDomas(workbook);
//        //获取工作表
//        HSSFSheet sheet = workbook.getSheetAt(0);
//        int lastRowNum = sheet.getLastRowNum();
//        for (int i=0;i<lastRowNum;i++)
//        {
//            //获取指定行
//            HSSFRow row = sheet.getRow(i);
//            if (row==null)
//                continue;
//            for (int j=0;j<row.getLastCellNum();j++)
//            {
//
//                HSSFCell cell = row.getCell(j);
//                if(cell==null)
//                    continue;
//                cell.setCellType(CellType.STRING);
//                System.out.println(cell.getStringCellValue());
//            }
//            System.out.println("-----------");
//
//
//        }
        return "添加成功";
    }
//    @RequestMapping("/doma/{page}")
//    public String reger(Model model, @PathVariable("page") Integer page)
//    {
//      model.addAttribute("page",page);
//        PageInfo<dorm> dormPageInfo = dormService.queryAllPages(page,30);
//        model.addAttribute("dorms",dormPageInfo.getList());
//        model.addAttribute("pages",dormPageInfo.getNavigatePages());
//        return"admin_dorm";
//
//    }
    //将宿舍信息打包成excel文件
     @GetMapping("/doma/DowendDomas")
    public  void  DowendDomas(HttpServletResponse response) throws IOException {
         System.out.println("--------导出宿舍信息-----");
         List<dorm> dorms = dormService.getdorms();
         String[] head={"公寓号","寝室号","楼层","寝室最大容量","剩余床位","寝室称号"};
         HSSFWorkbook sheets = new HSSFWorkbook();
         HSSFSheet sheet1 = sheets.createSheet("Sheet1");
         HSSFRow row_1 = sheet1.createRow(0);
         for (int i=0;i<head.length;i++)
         {
             row_1.createCell(i).setCellValue(head[i]);
         }
         for (int i=0;i<dorms.size();i++)
         {
             HSSFRow row = sheet1.createRow(i + 1);
             dorm dorm = dorms.get(i);

                 row.createCell(0).setCellValue(dorm.getApart_num());
             row.createCell(1).setCellValue(dorm.getDormi_num());
             row.createCell(2).setCellValue(dorm.getFloor());
             row.createCell(3).setCellValue(dorm.getMax_num());
             row.createCell(4).setCellValue(dorm.getNum());
             row.createCell(5).setCellValue(dorm.getTite());
         }
         sheets.setActiveSheet(0);
         response.setCharacterEncoding("utf-8");
         response.setContentType("multipart/form-data");
         response.setHeader("Content-Disposition","attachment;fileName=dorms.xls");
         ServletOutputStream outputStream = response.getOutputStream();
         sheets.write(outputStream);
         System.out.println("------导出成功--------");
         return ;
     }
}
